//
//  ViewController.m
//  高级课06数据持久化CoreData
//
//  Created by 邝子涵 on 15/11/20.
//  Copyright © 2015年 邝子涵. All rights reserved.
//

#import "ViewController.h"
#import "CoreDataManager.h"
#import "Student.h"
@interface ViewController ()
@property(nonatomic, strong) CoreDataManager *manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.manager = [CoreDataManager defaults];
    
    NSLog(@"url  ======= %@", [self.manager applicationDocumentsDirectory]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - 添加
- (IBAction)add:(id)sender {
    // 创建实体描述
    // 参数1: 描述的实体
    // 参数2: 数据管理器
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    // 创建实体
    // 参数1: 描述的实体
    // 参数2: 数据管理器
    Student *stu = [[Student alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    
    // 赋值
    stu.name = @"梁老虎";
    stu.age = @12;
    stu.sex = @"?";
    stu.number = @250;
    stu.hobby = @"吹🐂13";
    
    // 交钱
    [self.manager saveContext];
}



#pragma mark - 删除
- (IBAction)delete:(UIButton *)sender {
    // 获取查询数据的请求, 相当于数据库里的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    // 增加约束 谓词 是或者不是
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"梁老虎"];
    request.predicate = predicate;
    // 通过某个key给数组排序
    NSSortDescriptor *sortDesriotor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    // 排序
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDesriotor, nil]];
    
    NSError *error;
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    for (Student *stu in array) {
        [self.manager.managedObjectContext deleteObject:stu];
    }
    
    [self.manager saveContext];
}



#pragma mark - 修改
- (IBAction)update:(id)sender {
    // 获取查询数据的请求, 相当于数据库里的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    // 增加约束 谓词   是或者不是
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"梁老虎"];
    request.predicate = predicate;
    // 通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    // 排序
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    NSError *error;
    
    // 查找到的数组对象,通过一个数组显示出来
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    // 数组最后一个
    Student *stu = [array lastObject];
    stu.sex = @"不太确定性别!";
    
    // 进行保存
    [self.manager saveContext];
}



#pragma mark - 查询
- (IBAction)slect:(UIButton *)sender {
    // 获取查询数据的请求, 相当于数据库里的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    
    // 增加约束 谓词   是或者不是
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"梁老虎"];
    
    request.predicate = predicate;
    
    // 通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    // 排序
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    // 查找到的数组对象,通过一个数组显示出来
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    for (Student *stu in array) {
        NSLog(@"%@,%@,%@", stu.name, stu.sex, stu.hobby);
    }
}



@end
